package com.mindbook.vip.gawxchatinfo.service.impl.nostrum;

import com.mindbook.vip.gawxchatinfo.entity.nostrum.PrimaryLiverCancerEvaluatingSeverity;
import com.mindbook.vip.gawxchatinfo.entity.user.UserFront;
import com.mindbook.vip.gawxchatinfo.mapper.nostrum.PrimaryLiverCancerEvaluatingSeverityMapper;
import com.mindbook.vip.gawxchatinfo.service.nostrum.PrimaryLiverCancerEvaluatingSeverityService;
import com.mindbook.vip.gawxchatinfo.utils.IntToRomanUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @NAME: wxb
 * @DATE: 2022/8/5
 * @DESCRIPTION:
 **/
@Service
public class PrimaryLiverCancerEvaluatingSeverityServiceImpl implements PrimaryLiverCancerEvaluatingSeverityService {

    @Autowired
    private PrimaryLiverCancerEvaluatingSeverityMapper mapper;

    @Override
    public Integer removeByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public Integer add(PrimaryLiverCancerEvaluatingSeverity primaryLiverCancerEvaluatingSeverity) {
        return null;
    }

    @Override
    public Integer put(PrimaryLiverCancerEvaluatingSeverity primaryLiverCancerEvaluatingSeverity) {
        return null;
    }

    @Override
    public Integer addSelective(PrimaryLiverCancerEvaluatingSeverity primaryLiverCancerEvaluatingSeverity) {
        return null;
    }

    @Override
    public PrimaryLiverCancerEvaluatingSeverity findByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public Integer saveByPrimaryKeySelective(PrimaryLiverCancerEvaluatingSeverity primaryLiverCancerEvaluatingSeverity) {
        return null;
    }

    @Override
    public Integer saveByPrimaryKey(PrimaryLiverCancerEvaluatingSeverity primaryLiverCancerEvaluatingSeverity) {
        return null;
    }

    @Override
    public List<PrimaryLiverCancerEvaluatingSeverity> findAll() {
        return null;
    }

    @Override
    public Map<String, String> businessProcess(PrimaryLiverCancerEvaluatingSeverity p) {
        //将数据保存一下,完成个人用户信息的注册
        UserFront principal = (UserFront) SecurityUtils.getSubject().getPrincipal();
        p.setUserId(principal.getId());
        mapper.insertSelective(p);

        //创建返回去的数据对象map
        Map<String, String> map = new LinkedHashMap<>();
        //保存一下个人信息
        map.put("name",p.getName());
        map.put("age",p.getAge()+"");
        map.put("sex",p.getSex()+"");
        //病理诊断
        map.put("pathologicDiagnosis", pathologicDiagnosis(p));
        //Child-pugh分级
        map.put("childPughGrade", childPughGrade(p));
        //乙型肝炎
        map.put("hepatitisB", hepatitisB(p));
        //丙型肝炎
        map.put("hepatitisCAntibody", hepatitisCAntibody(p));
        //临床分期
        map.put("clinicalStages",clinicalStages(p));
        //TNM分期
        map.put("tnmStages",tnmStages(p));
        //巴塞罗那分期暂时不做

        //身体状况评分
        map.put("physicalCondition", physicalCondition(p));
        //麻醉ASA评分
        map.put("anesthesiaGrade", anesthesiaGrade(p));

        return map;
    }

    /**
     * TNM分期
     * @param p
     * @return
     */
    private String tnmStages(PrimaryLiverCancerEvaluatingSeverity p){
        //1.获取M远处转移
        Integer mDistantMetastasis = p.getmDistantMetastasis();

        if (mDistantMetastasis==3){
            return "IV期";
        }else{
            //获取N-区域淋巴腺
            Integer nRegionalLymphGland = p.getnRegionalLymphGland();
            if(nRegionalLymphGland==3){
                return "IIIC期";
            }

            //获取T-原发病灶
            Integer tPrimaryLesion = p.gettPrimaryLesion();
            switch (tPrimaryLesion){
                case 0:
                    return "0期";
                case 1:
                    return "I期";
                case 2:
                    return "II期";
                case 3:
                    return "IIIA期";
                case 4:
                    return "IIIB期";
            }

        }
        return "";
    }

    /**
     * 临床分期
     * @param p
     * @return
     */
    private String clinicalStages(PrimaryLiverCancerEvaluatingSeverity p) {
        //获取症状
        String symptom = p.getSymptom();
        //获取体征
        String sign = p.getSign();

        //亚临床期
        if (symptom == "" && sign == "") {
            return "I期";
        }

        //判断有明显恶液质、黄疸、腹水或远处转移之一者
        if(symptomHasPhaseIII(symptom)||signPhaseIII(sign)){
            return "III期";
        }

        return "II期";
    }

    private boolean signPhaseIII(String sign){
        String[] split = sign.split(",");
        for (int i = 0; i < split.length; i++) {
            if (split[i] != "4") {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断症状中是否有恶液质情况
     * 有则返回true，无则返回false
     * @param symptom
     * @return
     */
    private boolean symptomHasPhaseIII(String symptom) {
        String[] split = symptom.split(",");
        for (int i = 0; i < split.length; i++) {
            if (split[i] == "5") {
                return true;
            }
        }
        return false;
    }


    /**
     * 丙型肝炎
     *
     * @param p
     * @return
     */
    private String hepatitisCAntibody(PrimaryLiverCancerEvaluatingSeverity p) {
        Integer hepatitisCAntibody = p.getHepatitisCAntibody();
        if (hepatitisCAntibody == 0) {
            return "阳性";
        } else {
            return "阴性";
        }
    }

    /**
     * 乙型肝炎
     *
     * @param p
     * @return
     */
    private String hepatitisB(PrimaryLiverCancerEvaluatingSeverity p) {
        //获取表面抗原
        Integer hepatitisBSurfaceAntigen = p.getHepatitisBSurfaceAntigen();
        //乙肝表面抗体（抗-HBs）
        Integer surfaceAntibody = p.getSurfaceAntibody();
        //e抗原
        Integer eAntigen = p.geteAntigen();
        //e抗体
        Integer eAntibody = p.geteAntibody();
        //核心抗体
        Integer coreAntibody = p.getCoreAntibody();

        //大三阳的判断
        if (hepatitisBSurfaceAntigen + eAntigen + coreAntibody == 3) {
            return "大三阳";
        }
        //小三阳
        if (hepatitisBSurfaceAntigen + eAntibody + coreAntibody == 3) {
            return "小三阳";
        }
        //携带者
        if (hepatitisBSurfaceAntigen + coreAntibody == 2) {
            return "携带者";
        }
        //判断携带者
        if (surfaceAntibody + eAntibody + coreAntibody > 0) {
            return "乙肝痊愈";
        }
        return "无";
    }

    /**
     * child-pugh评分
     *
     * @param p
     * @return
     */
    private String childPughGrade(PrimaryLiverCancerEvaluatingSeverity p) {
        Integer totalPoints = 0;
        //肝性脑病
        Integer hepaticEncephalopathy = hepaticEncephalopathy(p.getHepaticEncephalopathy());
        //腹水
        Integer theDegreeOfAscites = theDegreeOfAscites(p.getTheDegreeOfAscites());
        //总胆红素(总胆红素的初始值就是1)
        Integer totalBilirubin = p.getTotalBilirubin();
        //白蛋白
        Integer serumAlbumin = p.getSerumAlbumin();
        //凝血酶原延长时间
        Integer prolongedClottingTime = p.getProlongedClottingTime();
        //现在总计Child-pugh评分
        totalPoints = hepaticEncephalopathy + theDegreeOfAscites + totalBilirubin + serumAlbumin + prolongedClottingTime;
        switch (totalPoints) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                return "Child A";
            case 7:
            case 8:
            case 9:
                return "Child B";
            default:
                return "Child C";
        }
    }


    /**
     * 腹水程度
     *
     * @param value
     * @return
     */
    private Integer theDegreeOfAscites(Integer value) {
        int points = 0;
        switch (value) {
            case 0:
                points = 1;
                break;
            case 1:
                points = 2;
                break;
            case 2:
            case 3:
                points = 3;
                break;
        }
        return points;
    }

    /**
     * 计算肝性脑病的评分
     *
     * @param value
     * @return
     */
    private Integer hepaticEncephalopathy(Integer value) {
        int points = 0;
        switch (value) {
            case 0:
                points = 1;
                break;
            case 1:
            case 2:
                points = 2;
                break;
            case 3:
            case 4:
                points = 3;
                break;
        }
        return points;
    }


    /**
     * 身体状况评分
     *
     * @param p
     * @return
     */
    private String physicalCondition(PrimaryLiverCancerEvaluatingSeverity p) {
        return p.getPhysicalCondition() + "分";
    }

    /**
     * 麻醉ASA分级
     *
     * @param p
     * @return 正确的评分 II期
     */
    private String anesthesiaGrade(PrimaryLiverCancerEvaluatingSeverity p) {
        Integer anesthesiaGrade = p.getAnesthesiaGrade();
        //将麻醉asa分级转换为罗马数字
        String s = IntToRomanUtils.intToRoman(anesthesiaGrade);
        return s + "期";
    }

    /**
     * 病理诊断
     *
     * @param p
     * @return 病理诊断结果
     */
    private String pathologicDiagnosis(PrimaryLiverCancerEvaluatingSeverity p) {
        String flag = "";
        //如果病理诊断为空，则返回空字符串
        String pathologicalPattern = p.getPathologicalPattern();
        String[] strArray = {"尚无病理结果", "肝细胞性肝癌", "胆管细胞性肝癌", "混合癌", "其它肝脏恶性肿瘤"};

        if (pathologicalPattern != "") {
            //将获取到的值按照逗号进行分隔
            String[] split = pathologicalPattern.split(",");
            for (int i = 0; i < split.length; i++) {
                flag += split[i] + ",";
            }
            //将最后的逗号去除
            flag = flag.substring(0, flag.length() - 1);
        }
        return flag;
    }


}
